package org.bluedream.core.module.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.core.config.advice.RsaSecurityParameter;
import org.bluedream.core.module.sys.config.entity.Config;
import org.bluedream.core.module.sys.entity.dict.DictData;
import org.bluedream.core.module.sys.entity.dict.DictType;
import org.bluedream.core.module.sys.service.dict.DictDataService;
import org.bluedream.core.module.sys.service.dict.DictTypeService;
import org.bluedream.core.utils.UserUtil;
import org.bluedream.core.utils.configUtil.GoalConfigUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @ClassName DictController
 * @Description TODO
 * @Author foxsand
 * @Data 2021-09-13 9:59
 * @Version
 */
@Slf4j
@Controller
@RequestMapping("${adminPath}/dict")
public class DictController extends BaseController {
    @Resource
    private DictDataService dictDataService;

    @Resource
    private DictTypeService dictTypeService;

    @ModelAttribute
    public DictType get(String id){
        return dictTypeService.getById(id);
    }

    @RequiresPermissions("dict:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(DictType  entity, Model model){
        model.addAttribute("isSystem" , UserUtil.isSystem());
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("dict" , entity);
        return "sys/dict/dictList";
    }

    @RequiresPermissions("dict:view")
    @RequestMapping("/form")
    public String form(DictType entity, Model model){
        model.addAttribute("dict" , entity);
        return "sys/dict/dictForm";
    }

    @RequiresPermissions("dict:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<DictType> findPage(@RsaSecurityParameter DictType entity){
        return dictTypeService.findPage(entity , "update_Date");
    }

    @RequiresPermissions("dict:edit")
    @RequestMapping("/save")
    @ResponseBody
    public String save(DictType entity){
        boolean flag = dictTypeService.save(entity);
        return "字典信息保存成功";
    }

    @RequiresPermissions("dict:view")
    @RequestMapping(value = {"/dataList"})
    public String dataList(DictType  entity, Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("dictData" , new DictData(entity.getDictType() , entity.getOrgCode()));
        return "sys/dict/dictDataList";
    }

    @RequiresPermissions("dict:view")
    @RequestMapping("/findPage2Data")
    @ResponseBody
    public IPage<DictData> findPage2Data(@RsaSecurityParameter DictData entity){
        return dictDataService.findPage(entity , "sort");
    }

    @RequiresPermissions("dict:view")
    @RequestMapping("/form2Data")
    public String form2Data(DictData entity, Model model){
        entity = dictDataService.getDictData(entity);
        model.addAttribute("dictData" , entity);
        return "sys/dict/dictDataForm";
    }

    @RequiresPermissions("dict:edit")
    @RequestMapping("/save2Data")
    @ResponseBody
    public String save2Data(DictData entity){
        boolean flag = dictDataService.save(entity);
        return "字典信息保存成功";
    }
}
